
const fetchAll = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.all(query, params, (err, rows) => {
            if (err) return reject(err);
            resolve(rows);
        });
    });
}

const findAll = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.all(query, params, (err, rows) => {
            if (err) return reject(err);
            resolve(rows);
        });
    });
}

const findOne = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.get(query, params, (err, row) => {
            if (err) return reject(err);
            resolve(row);
        });
    });
}

const fetchOne = async (query, params) => { 
    return new Promise((resolve, reject) => {
        global.db.get(query, params, (err, row) => {
            if (err) return reject(err);
            resolve(row);
        });
    });
}

const insert = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.run(query, params, function(err) {
            if (err) return reject(err);
            resolve({ id: this.lastID });
        });
    });
}

const update = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.run(query, params, (err) => {
            if (err) return reject(err);
            resolve();
        });
    });
}   
const del = async (query, params) => {
    return new Promise((resolve, reject) => {
        global.db.run(query, params, (err) => {
            if (err) return reject(err);
            resolve();
        });
    });
}

module.exports = { fetchAll, findAll, findOne, fetchOne, insert, update, del };  